<template>
	<view>
		<view class="cu-bar bg-white search " :style="[{top:0 + 'px'}]">
			<view class="search-form round">
				<text class="cuIcon-search"></text>
				<input type="text" placeholder="输入搜索的手机号/群号" v-model="userFriend.userMobile" confirm-type="search" @confirm="search"></input>
			</view>
			<view class="action">
				<button class="cu-btn bg-gradual-blue shadow-blur round" @tap="search" v-no-more-click :disabled="$store.state.disabled">搜索</button>
			</view>
		</view>
		<scroll-view scroll-y class="indexes" :scroll-into-view="'indexes-'+ listCurID" :style="[{height:'calc(100vh - '+ CustomBar + 'px - 50px)'}]"
		 :scroll-with-animation="true" :enable-back-to-top="true">
			<block v-for="(item,index) in list" :key="index">
				<view @tap="toPreView()" :class="'indexItem-' + item.userNikename" :id="'indexes-' + item.userNikename" :data-index="item.userNikename">
					<view class="cu-list menu-avatar no-padding">
						<view class="cu-item">
							<view class="cu-avatar round lg" :style="'background-image:url('+item.userHeadPortrait+'),url(/static/img/none.jpg);'"/>
							<view class="content">
								<view class="text-grey">{{item.userNikename}}</view>
								<view class="text-gray text-sm">
									{{item.userMobile}} | {{item.userSex==0?'男':'女'}}
								</view>
							</view>
							<view class="action" id="action">
								<view class="cu-tag round bg-green" @click.stop="preAddFriend(item,index)" v-no-more-click :disabled="$store.state.disabled">加好友</view>
							</view>
						</view>
					</view>
				</view>
			</block>
		</scroll-view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				type:0,  //0:好友
				CustomBar: this.CustomBar,
				hidden: true,
				listCurID: '',
				list: [],
				listCur: '',
				userFriend:{},
				rule: [
					{
						name: 'userMobile',
						checkType: 'phoneno',
						checkRule: '',
						errorMsg: '请填写正确的手机号'
					}
				]
			};
		},
		onLoad(parm){
			if(parm.type){
				this.type=parseInt(parm.type);
			}
		},
		methods: {
			async search(){
				//添加好友
				if(this.type===0){
					if (!this.dataCheck.check(this.userFriend, this.rule)) {
						uni.showToast({
							title: this.dataCheck.error,
							icon: 'none'
						});
						return;
					}
					let res = await this.$request.get('/user-admin/user/searchByMobile', {
						data: {userMobile:this.userFriend.userMobile}
					});
					console.log(res);
					res.data.userHeadPortrait=this.getImgUrl('user',res.data.userId);
					this.$set(this.list,0,res.data);
				}
			},
			toPreView() {
				uni.navigateTo({
					url: '/pages/chat/pre-view'
				});
			},
			preAddFriend(item,index){
				const user=this.$store.state.userInfo;
				let msg={
				   "from": '0',
				   "to": item.userId.toString(),
				   "cmd":11,
				   "createTime": new Date().getTime(),
				   "msgType": 0,
				   "chatType":0, //0:系统消息,1:公聊,2:私聊
				   "content": "添加好友",
				   "extras":{
					   title:"验证消息",
					   from:user.userId+'',
					   userNikename:user.userNikename,
					   userMobile:user.userMobile
				    }
				};
				if(msg.extras.from===msg.to){
					uni.showToast({
						title: '不能添加自己',
						icon: 'none'
					});
					return;
				}
				this.ws.socketTask.send({
					data: JSON.stringify(msg),//将json对象转换成字符串, 
					success: ()=> {
						console.log('socket send success')
						uni.showToast({
							title: '已发送请求消息',
							icon: 'none'
						});
						this.list.splice(index,1);
					},
					fail: ()=> {
						console.log('socket send fail')
					}
				});
			},
		}
	}
</script>

<style>
</style>
